#####################################################
# RIGHTEOUS BY RESCUER STATUS AND COUNTRY OF RESCUE #
#####################################################

# Author: Kasia Nalewajko
# First created: 15 November 2022
# Replicated: 17 June 2024

rm(list = ls())

# LOAD PACKAGES -----------------------------------------------------------

if (!require("dplyr")) install.packages("dplyr")
if (!require("ggplot2")) install.packages("ggplot2")
if (!require("viridis")) install.packages("viridis")

# LOAD DATA ---------------------------------------------------------------

load("./00 SUBMITTED/00 APSR final/04 replication_files/01 data/RaN.Rda")

# PLOT --------------------------------------------------------------------

# plot not individuals, but rescuer households
unique_rans <- ran %>% 
  dplyr::select(hid, file_no, rescuers_no, status, rescuees_no, rescue_country, rescue_loc) %>% 
  unique()

unique_rans$rescue_country <- ifelse(unique_rans$rescue_country == "Russia", "USSR", unique_rans$rescue_country)

only_big <- unique_rans %>% 
  group_by(rescue_country) %>% 
  summarise(rescuers_sum = n()) %>% 
  filter(!is.na(rescue_country) & rescuers_sum > 100) %>% 
  arrange(desc(rescuers_sum))

only_big <- unlist(only_big$rescue_country)  

unique_rans %>% 
  dplyr::filter(rescue_country %in% only_big) %>%
  ggplot() +
  geom_bar(position = "stack") +
  aes(x = reorder(rescue_country, -table(rescue_country)[rescue_country])) +
  aes(fill = status) +
  scale_fill_viridis_d(option = "inferno", begin = 0.2, end = 0.85) +
  theme_bw() +
  theme(axis.text.x = element_text(angle = 45, hjust = 1, vjust = 1),
        legend.position = "bottom") +
  labs(x = "Country",
       y = "Number of rescuer households",
       fill = "Rescuer status")

# EXPORT --------------------------------------------------------------------

ggsave(
  "E1_righteous_country.png",
  plot = last_plot(),
  path = "./00 SUBMITTED/00 APSR final/03 dataverse_online_appendix/figures",
  width = 15,
  height = 10,
  units = "cm",
  dpi = 300
)

